/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const tools = require("../js/tools");

window.Action_question_Create = function (question, analysis, a, b, c, d, isA, isB, isC, isD, isTrue, type, level, bankUUID) {
    let q = {
        "QuestionBase64": tools.Base64(question),
        "AnalysisBase64": tools.Base64(analysis),
        "AnswerBase64A": tools.Base64(a),
        "AnswerBase64B": tools.Base64(b),
        "AnswerBase64C": tools.Base64(c),
        "AnswerBase64D": tools.Base64(d),
        "IsA": isA,
        "IsB": isB,
        "IsC": isC,
        "IsD": isD,
        "IsTrue": isTrue,
        "Type": type,
        "Level": level,
        "UUID": ""
    };

    let request = tools.DeepCopy(window.API_Request_Question_CreateQuestion);
    request.Question = q;
    request.BankUUID = bankUUID;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Question_CreateQuestion,
        requestLoad
    );
};

window.Action_question_Delete = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Question_DeleteQuestion);
    request.WillDeleteUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Question_DeleteQuestion,
        requestLoad
    );
};

window.Action_question_Get = function (uuid) {
    let request = tools.DeepCopy(window.API_Request_Question_GetQuestion);
    request.QuestionUUID = uuid;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Question_GetQuestion, [
            request.QuestionUUID,
            request.AuthToken
        ]),
    );
};

window.Action_question_List = function (bankUUID) {
    let request = tools.DeepCopy(window.API_Request_Question_ListQuestion);
    request.QuestionBankUUID = bankUUID;
    request.AuthToken = window.AuthToken;

    return tools.XHR(
        window.API_METHOD_GET,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        tools.StrFormat(window.API_URL_Question_ListQuestion, [
            request.QuestionBankUUID,
            request.AuthToken
        ]),
    );
};

window.Action_question_Show = function (uuidList) {
    let request = tools.DeepCopy(window.API_Request_Question_ShowQuestions);
    request.QuestionUUIDList = uuidList;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Question_ShowQuestions,
        requestLoad
    );
};

window.Action_question_Update = function (uuid, question, analysis, a, b, c, d, isA, isB, isC, isD, isTrue, type, level) {
    let q = {
        "QuestionBase64": tools.Base64(question),
        "AnalysisBase64": tools.Base64(analysis),
        "AnswerBase64A": tools.Base64(a),
        "AnswerBase64B": tools.Base64(b),
        "AnswerBase64C": tools.Base64(c),
        "AnswerBase64D": tools.Base64(d),
        "IsA": isA,
        "IsB": isB,
        "IsC": isC,
        "IsD": isD,
        "IsTrue": isTrue,
        "Type": type,
        "Level": level,
        "UUID": ""
    };

    let request = tools.DeepCopy(window.API_Request_Question_UpdateQuestion);
    request.Question = q;
    request.WillUpdateUUID = uuid;
    request.AuthToken = window.AuthToken;

    let requestLoad = JSON.stringify(request);

    return tools.XHR(
        window.API_METHOD_POST,
        window.API_HOST_SCHEMES,
        window.API_HOST_HOST,
        window.API_URL_Question_UpdateQuestion,
        requestLoad
    );
};
